﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class XiuXingView : MonoBehaviour
{

    public Button closeBtn;
    public GameObject SubLevelPage;

    public Text m_text1;
    public Text m_text2;
    public Text m_text3;
    public Text m_text4;
    public Text m_text5;

    public List<Button> subCloseBtns;
    public List<Button> m_levelBtns;
    public List<Button> m_btns;

    public void OnShow()
    {
        closeBtn.onClick.AddListener(() =>
        {
            UIManager.Instance.ShowPage(UINames.DongfuPage);
            MessageCenter.Instance.SendMessage(MessageConsts.SHOW_DONG_FU);
        });

        foreach (var btn in subCloseBtns)
        {
            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(() =>
            {
                SubLevelPage.SetActive(false);
            });
        }

        for (int i = 0; i < m_btns.Count; i++)
        {
            var btn = m_btns[i];
            btn.onClick.RemoveAllListeners();
            int chapt = m_btns.Count - i;
            btn.onClick.AddListener(() =>
            {
                ShowSubView(chapt);
            });
        }
        RefreshPage();
    }

    public void OnHide()
    {
        closeBtn.onClick.RemoveAllListeners();
    }

    private void RefreshPage()
    {
        SubLevelPage.SetActive(false);
    }

    private void ShowSubView(int clickChapter)
    {
        var level = XiuXingModel.Instance.curLevel;
        var chapter = XiuXingModel.Instance.curChapter;
        if (clickChapter < chapter)
            level = int.MaxValue;
        if (clickChapter > chapter)
            level = int.MinValue;

        var list = new List<Text>()
        {
            m_text1,
            m_text2,
            m_text3,
            m_text4,
            m_text5,
        };
        for (int i = 0; i < list.Count; i++)
        {
            var btn = m_levelBtns[i];
            var texgt = list[i];
            texgt.color = i + 1 < level ? new Color(157 / 255f, 236 / 255f, 70 / 255f) : i + 1 == level ? Color.white : Color.gray;
            if (i + 1 <= level)
            {
                btn.onClick.AddListener(() => { PlayLevel(clickChapter, i + 1); });
            }
            else
            {
                btn.onClick.RemoveAllListeners();
            }
        }
        SubLevelPage.SetActive(true);
    }

    private void PlayLevel(int chapter, int level)
    {
        UIManager.Instance.ShowPage(UINames.BattleScene);
        MessageCenter.Instance.SendMessage(MessageConsts.BATTLE_START, new object[] { chapter, level });
    }
}
